home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicProgressBarUI.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  247 lines

  1. /*
  2.  * @(#)BasicProgressBarUI.java    1.37 98/04/11
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import java.awt.*;
  24. import com.sun.java.swing.*;
  25. import com.sun.java.swing.event.*;
  26. import com.sun.java.swing.plaf.*;
  27. import java.io.Serializable;
  28.  
  29.  
  30. /**
  31.  * A Basic L&F implementation of ProgressBarUI.
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @version 1.37 04/11/98
  41.  * @author Michael C. Albers
  42.  */
  43. public class BasicProgressBarUI extends ProgressBarUI
  44.     implements ChangeListener, Serializable {
  45.  
  46.     private int cachedPercent;
  47.     private static final Dimension PREFERRED_INNER_HORIZONTAL = new Dimension(146, 12);
  48.     private static final Dimension PREFERRED_INNER_VERTICAL = new Dimension(12, 146);
  49.     protected static int cellLength;
  50.     protected static int cellSpacing;
  51.  
  52.  
  53.     public static ComponentUI createUI(JComponent x) {
  54.     return new BasicProgressBarUI();
  55.     }
  56.  
  57.     public void installUI(JComponent c) {
  58.     installDefaults(c);
  59.     installListeners(c);
  60.     }
  61.  
  62.     public void uninstallUI(JComponent c) {
  63.     uninstallListeners(c);
  64.     }
  65.   
  66.     protected void installDefaults(JComponent c) {
  67.     c.setOpaque(true);
  68.      LookAndFeel.installBorder(c,"ProgressBar.border");    
  69.     LookAndFeel.installColorsAndFont(c, "ProgressBar.background",
  70.                      "ProgressBar.foreground",
  71.                      "ProgressBar.font");
  72.     cellLength = ((Integer)UIManager.get("ProgressBar.cellLength")).intValue();
  73.     cellSpacing = ((Integer)UIManager.get("ProgressBar.cellSpacing")).intValue();
  74.     }
  75.     
  76.     protected void installListeners(JComponent c) {
  77.     ((JProgressBar)c).addChangeListener(this);
  78.     }    
  79.  
  80.     protected void uninstallListeners(JComponent c) {
  81.     ((JProgressBar)c).removeChangeListener(this);
  82.     }
  83.     
  84.     protected void uninstallDefaults(JComponent c) {
  85.      LookAndFeel.uninstallBorder(c);    
  86.     }
  87.  
  88.     public Dimension getPreferredInnerHorizontal() {
  89.     return PREFERRED_INNER_HORIZONTAL;
  90.     }
  91.  
  92.     public Dimension getPreferredInnerVertical() {
  93.     return PREFERRED_INNER_VERTICAL;
  94.     }
  95.  
  96.     public int getCachedPercent() {
  97.     return cachedPercent;
  98.     }
  99.  
  100.     public void setCachedPercent(int cachedPercent) {
  101.     this.cachedPercent = cachedPercent;
  102.     }
  103.  
  104.     /**
  105.      * This determines the amount of the progress bar that should be filled
  106.      * based on the percent done gathered from the model. This is a common
  107.      * operation so it was abstracted out. It assumes that your progress bar
  108.      * is linear. That is, if you are making a circular progress indicator,
  109.      * you will want to override this method.
  110.      */
  111.     public int getAmountFull(JComponent c) {
  112.     int amountFull = 0;
  113.     JProgressBar progressBar = (JProgressBar)c;
  114.     BoundedRangeModel model = progressBar.getModel();
  115.     int width = c.getWidth();
  116.     int height = c.getHeight();
  117.     Insets b = progressBar.getInsets(); // effectively the border
  118.     int x = b.left;
  119.     int y = b.top;
  120.  
  121.     long span = model.getMaximum() - model.getMinimum();
  122.     if (span != 0) {
  123.         double bigValue = model.getValue();
  124.         double fractionComplete = bigValue / span;
  125.         if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  126.             double bigWidth = width - (b.left + b.right);
  127.             amountFull = (int)(bigWidth * fractionComplete);
  128.         } else {
  129.             double bigHeight = height - (b.top + b.bottom);
  130.             amountFull = (int)(bigHeight * fractionComplete);
  131.         }
  132.     }
  133.     return amountFull;
  134.     }
  135.  
  136.     /**
  137.      * All purpose paint method that should do the right thing for almost
  138.      * all linear progress bars. By setting a few values in the defaults
  139.      * table, things should work just fine to paint your progress bar.
  140.      * Naturally, override this if you are making a circular or
  141.      * semi-circular progress bar.
  142.      */
  143.     public void paint(Graphics g, JComponent c) {
  144.     JProgressBar progressBar = (JProgressBar)c;
  145.     BoundedRangeModel model = progressBar.getModel();
  146.  
  147.     Dimension size = progressBar.getSize(); //total size
  148.     Insets i = progressBar.getInsets(); // area for border
  149.     Rectangle barRect = new Rectangle(size); // area to draw progress
  150.  
  151.     barRect.x += i.left;
  152.     barRect.y += i.top;
  153.     barRect.width -= (i.right + barRect.x);
  154.     barRect.height -= (i.bottom + barRect.y);
  155.  
  156.     int current;
  157.     int increment = cellLength + cellSpacing; // a cell and its spacing
  158.     int amountFull = getAmountFull(c); // amount of progress to draw
  159.  
  160.     g.setColor(c.getForeground());
  161.     if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  162.         // the largest number to draw a cell at
  163.         int max = (barRect.x + amountFull) - cellLength;
  164.         
  165.         // draw the cells
  166.         for (current = barRect.x; current <= max; current += increment) {
  167.         g.fillRect(current, barRect.y, cellLength, barRect.height);
  168.         }
  169.  
  170.     } else {
  171.         // the smallest number to draw a cell at
  172.         //  that is, the number at the top
  173.         int min = barRect.height - amountFull;
  174.  
  175.         // draw the cells
  176.         // note that this has a certain inefficiency at 100% where
  177.         //  it will draw outside of it's actual area.
  178.         for (current = barRect.height; current >= min;
  179.          current -= increment)
  180.         {
  181.         g.fillRect(barRect.x, current, barRect.width, cellLength);
  182.         }
  183.     }
  184.     }
  185.  
  186.     public Dimension getPreferredSize(JComponent c) {
  187.     Dimension    size;
  188.     JProgressBar    progressBar = (JProgressBar)c;
  189.     Insets        border = progressBar.getInsets();
  190.  
  191.     if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  192.         size = new Dimension(getPreferredInnerHorizontal());
  193.     } else {
  194.         size = new Dimension(getPreferredInnerVertical());
  195.     }
  196.  
  197.     size.width += border.left + border.right;
  198.     size.height += border.top + border.bottom;
  199.     return size;
  200.     }
  201.  
  202.     public Dimension getMinimumSize(JComponent c) {
  203.     Dimension pref = getPreferredSize(c);
  204.     JProgressBar progressBar = (JProgressBar)c;
  205.     if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  206.         pref.width = 0;
  207.     } else {
  208.         pref.height = 0;
  209.     }
  210.     return pref;
  211.     }
  212.  
  213.     public Dimension getMaximumSize(JComponent c) {
  214.     Dimension pref = getPreferredSize(c);
  215.     JProgressBar progressBar = (JProgressBar)c;
  216.     if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
  217.         pref.width = Short.MAX_VALUE;
  218.     } else {
  219.         pref.height = Short.MAX_VALUE;
  220.     }
  221.     return pref;
  222.     }
  223.  
  224.  
  225.     //
  226.     // Change Events
  227.     //
  228.     public void stateChanged(ChangeEvent e) {
  229.     JProgressBar bar = (JProgressBar)e.getSource();
  230.     BoundedRangeModel model = bar.getModel();
  231.     int newRange = model.getMaximum() - model.getMinimum();
  232.     int newPercent;
  233.     int oldPercent = getCachedPercent();
  234.  
  235.     if (newRange > 0) {
  236.         newPercent = (100 * model.getValue()) / newRange;
  237.     } else {
  238.         newPercent = 0;
  239.     }
  240.  
  241.     if (newPercent != oldPercent) {
  242.         setCachedPercent(newPercent);
  243.         bar.repaint();
  244.     }
  245.     }
  246. }
  247.